package com.example.homework1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,bdot,bequal,b10,b12;
    TextView ans;
    double var1,var2;
    boolean add,sub,mul,div,n10;
    boolean sq = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1= (Button) findViewById(R.id.btn1);
        b2= (Button) findViewById(R.id.btn2);
        b3= (Button) findViewById(R.id.btn3);
        b4= (Button) findViewById(R.id.btn4);
        b5= (Button) findViewById(R.id.btn5);
        b6= (Button) findViewById(R.id.btn6);
        b7= (Button) findViewById(R.id.btn7);
        b8= (Button) findViewById(R.id.btn8);
        b9= (Button) findViewById(R.id.btn9);
        b0= (Button) findViewById(R.id.btn0);
        badd= (Button) findViewById(R.id.btnadd);
        bsub= (Button) findViewById(R.id.btnsub);
        bmul= (Button) findViewById(R.id.btnmul);
        bdiv= (Button) findViewById(R.id.btndiv);
        bdot= (Button) findViewById(R.id.btndot);
        bequal= (Button) findViewById(R.id.btnequel);
        b10= (Button) findViewById(R.id.btn10);
        b12= (Button) findViewById(R.id.btn12);
        ans = (TextView) findViewById(R.id.Answer);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"1");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"1");

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"2");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"2");
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"3");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"3");
            }
        });
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"4");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"4");
            }
        });
        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"5");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"5");
            }
        });
        b6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"6");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"6");
            }
        });
        b7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"7");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"7");
            }
        });
        b8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"8");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"8");
            }
        });
        b9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true) {
                    ans.setText(null);
                    ans.setText(ans.getText()+"9");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"9");
            }
        });
        b0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"0");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"0");
            }
        });
        bdot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(ans.getText().toString().contains("."))
                        ans.setText(ans.getText()+"");
                    else
                        ans.setText(ans.getText()+".");
                } catch (Exception e) {
                    ans.setText("出错");
                }
            }
        });
        badd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    add=true;
                    ans.setText(null);
                }catch (RuntimeException a){
                    ans.setText("错误");
                    sq=true;

                }
            }
        });
        bsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    sub=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        bmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    mul=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        bdiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*var1 = Double.parseDouble(ans.getText()+"");
                div=true;
                ans.setText(null);*/
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    div=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        b10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ans.setText(null);
                add=false;sub=false;mul=false;div=false;
            }
        });
        b12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String temp = "";
                    temp = ans.getText().toString().substring(0, ans.length() - 1);
                    ans.setText(temp + "");
                }catch(RuntimeException a){
                    ans.setText("错误");
                }
            }
        });
        bequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var2 = Double.parseDouble(ans.getText() + "");
                    if (add == true) {
                        ans.setText(var1 + var2 + "");
                        add = false;
                        sq = true;
                    }
                    if (sub == true) {
                        ans.setText(var1 - var2 + "");
                        sub = false;
                        sq = true;
                    }
                    if (mul == true) {
                        ans.setText(var1 * var2 + "");
                        mul = false;
                        sq = true;
                    }
                    if (div == true) {
                        ans.setText(var1 / var2 + "");
                        div = false;
                        sq = true;
                    }
                    if (n10 == true) {
                        ans.setText(0 + "");
                        n10 = false;
                        sq = true;
                    }
                }catch(RuntimeException a){
                }

            }
        });

//        版权声明：本文为CSDN博主「明天过后哈哈哈」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/weixin_57398221/article/details/125072470







    }
}